/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package agenda.calendario;

import agenda.calendario.DAL.CompromissoDAL;
import framework.agendaFactory.ButtonListener;
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;

/**
 *
 * @author Sergio Carlos Peres
 */
public class CompromissoButtonListener extends ButtonListener{
    
    public CompromissoButtonListener(framework.agendaFactory.lib.InternalFrame internalFrame) {
        super(internalFrame);
    }
    
    @Override
    public void txtPesquisarActionPerformed(ActionEvent e) {
        pesquisar("item", "compromisso");
    }

    @Override
    public void btnPesquisarActionPerformed(ActionEvent e) {
        pesquisar("item", "compromisso");
    }

    @Override
    public void cboPesquisarActionPerformed(ActionEvent e) {
        pesquisaCombobox("item", "compromisso");
    }

    @Override
    public void btnAlterarActionPerformed(ActionEvent e) {
        int linha;
        linha = internalFrame.getTabela().getSelectedRow();
        if (linha == -1) {
            JOptionPane.showMessageDialog(null, "Selecione um compromisso da lista de\ncompromisso para altera-lo!");
            return;
        }
        Compromisso compromisso = new Compromisso();
        compromisso.setCompomisso(internalFrame.getTabela().getValueAt(linha, 1).toString());
        compromisso.setData(
                framework.utilitarios.Data.retornaData(
                    internalFrame.getTabela().getValueAt(linha, 2).toString(), 
                    internalFrame.getTabela().getValueAt(linha, 3).toString()
                )
        );
        this.altere = true;
        interfacePadrao("novo", true, 1);
        internalFrame.setTelaAdd(new CompromissoDAL().buscaAlterar(compromisso));
    }
    
    @Override
    public void btnSalvarActionPerformed(ActionEvent e) {
        Compromisso compromisso = (Compromisso) internalFrame.getTelaAdd();
        
        if (this.altere)
            this.internalFrame.getStrategy().alterarItem(compromisso);
        else 
            this.internalFrame.getStrategy().addItem(compromisso);
        
        this.altere = false;
        interfacePadrao(internalFrame.getFactory().getSQL(), "", false, 0);
    }

    @Override
    public void btnExcluirActionPerformed(ActionEvent e) {
        int linha;
        linha = internalFrame.getTabela().getSelectedRow();
        if (linha == -1) {
            JOptionPane.showMessageDialog(null, "Selecione um compromisso da lista de\ncompromisso para exclui-lo!");
            return;
        }
        Compromisso c = new Compromisso();
        c.setCompomisso(internalFrame.getTabela().getValueAt(linha, 1).toString());
        c.setData(
                framework.utilitarios.Data.retornaData(
                    internalFrame.getTabela().getValueAt(linha, 2).toString(), 
                    internalFrame.getTabela().getValueAt(linha, 3).toString()
                )
        );
        c = (Compromisso) new CompromissoDAL().buscaAlterar(c);
        this.internalFrame.getStrategy().removeItem(c);
        interfacePadrao(internalFrame.getFactory().getSQL(),"", false, 0);
    }

    @Override
    public void adicionalActionPerformaed(ActionEvent e, String nomeVariavel) {
        switch (nomeVariavel) {
            case "rbnDia"          : rbnDiaActionPerformed(e);          break;
            case "rbnConcluido"    : rbnConcluidoActionPerformed(e);    break;
            case "rbnNaoConcluido" : rbnNaoConcluidoActionPerformed(e); break;
            case "rbnSeleciona"    : rbnSelecionaActionPerformed(e);    break;
        }
    }
    
    public void rbnDiaActionPerformed(ActionEvent e){
        internalFrame.getFactory().setSQL("SELECT * FROM item "
                + "WHERE status = 'F' and "
                + "datahora >= '" + framework.utilitarios.Data.retona() 
                + "' ORDER BY datahora");
        internalFrame.preencherTabela(internalFrame.getFactory().getSQL());
        internalFrame.ativarCamposAdicionar(false);
    }

    private void rbnConcluidoActionPerformed(ActionEvent e) {
        internalFrame.getFactory().setSQL("SELECT * FROM item "
                + "WHERE status = 'T' ORDER BY datahora");
        internalFrame.preencherTabela(internalFrame.getFactory().getSQL());
        internalFrame.ativarCamposAdicionar(false);
    }

    private void rbnNaoConcluidoActionPerformed(ActionEvent e) {
        internalFrame.getFactory().setSQL("SELECT * FROM item "
                + "WHERE status = 'F' ORDER BY datahora");
        internalFrame.preencherTabela(internalFrame.getFactory().getSQL());
        internalFrame.ativarCamposAdicionar(false);
    }

    private void rbnSelecionaActionPerformed(ActionEvent e) {
        internalFrame.ativarCamposAdicionar(false);
    }
}
